package com.sunda.spmsweb.usercontroller;

import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsuser.entity.SpmsUser;
import com.sunda.spmsuser.service.*;
import com.sunda.spmsweb.aspect.Login;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmsweb.util.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "用户登录控制", description = "用户登录控制")
public class LoginController {

    @Autowired
    ISpmsUserService iSpmsUserService;

    @Autowired
    ISpmsRoleService iSpmsRoleService;

    @Autowired
    ISpmsPermissionService iSpmsPermissionService;

    @Autowired
    ISpmsUserWorkshopService iSpmsUserWorkshopService;

    @Autowired
    ISpmsUserWhsService iSpmsUserWhsService;

    /** @Login 监控登录用户记录日志; 用户登录，成功则返回用户相关信息——用户个人信息、角色、权限、工厂、车间 配置信息 */
    @Login(operateType = "登录")
    @RequestMapping(value = "/login")
    @ApiOperation(value = "用户登陆获取 token 信息",notes = "", httpMethod = "POST")
    public ResponseResult login(String userId,
                                String password) {
        String mdpassword = MD5Utils.encrypt(userId, password);
        SpmsUser user = iSpmsUserService.getByUserId(userId);
        if (user == null){
            return ResponseResult.error("用户不存在");
        }else if (user.getSpmsStatus().equals("0")){
            return ResponseResult.error("账户已锁定，请联系管理员！");
        }else if (!mdpassword.equals(user.getLoginPassword())){
            return ResponseResult.error("密码错误请重试！");
        }
        List<Map<String, Object>> roleList = iSpmsRoleService.getRoleById(userId);
        List<String> list = new ArrayList<>();
        if (roleList.size() > 0){
            for (Map<String, Object> map : roleList) {
                list.add(map.get("roleId").toString());
            }
        }
        return ResponseResult.success()
                .add("userInfo", user)
                .add("userRole", roleList)
                .add("userPerms", iSpmsPermissionService.getPermsByUserId(userId))
                .add("token", JWTUtil.createToken(userId, String.join(",", list)))
                .add("userWhs", iSpmsUserWhsService.getUserWhsList(userId))
                .add("userWorkshop", iSpmsUserWorkshopService.getUserWorkshopList(userId));
    }

//    @RequestMapping(value = "/logout")
//    @ApiOperation(value = "退出登录",notes = "退出登录", httpMethod = "GET")
//    public ResponseResult logout() {
//        //使用权限管理工具进行用户的退出，跳出登录，给出提示信息
//        Subject subject = SecurityUtils.getSubject();
//        if (subject.isAuthenticated()) {
//            subject.logout();
//        }
//        return ResponseResult.success().add("msg", "用户已退出登录！");
//    }

//    @RequestMapping(value = "/err")
//    @ApiOperation(value = "错误请求路由",notes = "错误请求路由", httpMethod = "GET")
//    public ResponseResult error(String msg) {
//        return ResponseResult.error(msg);
//    }
//
//    @RequestMapping(value = "/unauthorized")
//    @ApiOperation(value = "校验不通过请求路由",notes = "校验不通过请求路由", httpMethod = "GET")
//    public ResponseResult unauthorized(String msg) {
//        return ResponseResult.error(msg);
//    }


}
